<template>
  <div class="upload_cover">
    <!-- <h2 class="nav_title">就业信息上传</h2> -->
    <el-form
      :model="newUserInfo"
      :rules="rules"
      ref="newUserInfo"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="名字" prop="work_username">
        <el-input
          placeholder="不输入默认使用匿名用户"
          v-model="newUserInfo.work_username"
        ></el-input>
      </el-form-item>
      <el-form-item label="学号" prop="work_xuehao" v-if="!this.$store.state.userdata.shengfen==0">
        <el-input
          placeholder="请输入学号"
          v-model="newUserInfo.work_xuehao"
        ></el-input>
      </el-form-item>
 
      <el-form-item label="学制" prop="work_xuezhi">
        <el-select v-model="newUserInfo.work_xuezhi" placeholder="请选择学制">
          <el-option label="3年" value="3年"></el-option>
          <el-option label="4年" value="4年"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学历" prop="work_xueli">
        <el-select v-model="newUserInfo.work_xueli" placeholder="请选择学历">
          <el-option label="本科" value="本科"></el-option>
          <el-option label="硕士生毕业" value="硕士生毕业"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="work_sex">
        <el-select v-model="newUserInfo.work_sex" placeholder="请选择性别">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属专业" prop="work_zhuanye">
        <el-cascader
          v-model="newUserInfo.work_zhuanye"
          :options="options1"
        ></el-cascader>
      </el-form-item>
           <el-form-item label="毕业时间" prop="work_biye_time">
        <el-select
          v-model="newUserInfo.work_biye_time"
          placeholder="请选择毕业时间"
        >
          <el-option label="2020" value="2020"></el-option>
          <el-option label="2019" value="2019"></el-option>
          <el-option label="2018" value="2018"></el-option>
          <el-option label="2017" value="2017"></el-option>
          <el-option label="2016" value="2016"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="培养方式" prop="work_fangshi">
        <el-select
          v-model="newUserInfo.work_fangshi"
          placeholder="请选择培养方式"
        >
          <el-option label="统分" value="统分"></el-option>
          <el-option label="非定向" value="非定向"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="困难级别" prop="work_kun_grade">
        <el-select
          v-model="newUserInfo.work_kun_grade"
          placeholder="请选择困难级别"
        >
          <el-option label="家庭困难" value="家庭困难"></el-option>
          <el-option label="非困难生" value="非困难生"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="就业情况" prop="work_jiuye">
        <el-select
          v-model="newUserInfo.work_jiuye"
          placeholder="请选择就业情况"
        >
          <el-option label="待就业" value="待就业"></el-option>
          <el-option label="就业" value="就业"></el-option>
          <el-option label="升学" value="升学"></el-option>
          <el-option label="灵活就业" value="灵活就业"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="单位名称" prop="work_danwei">
        <el-input
          placeholder="不填，默认是升学"
          v-model="newUserInfo.work_danwei"
        ></el-input>
      </el-form-item>

      <el-form-item label="单位地址" prop="work_dizhi">
        <el-cascader
          size="large"
          :options="options"
          v-model="newUserInfo.work_dizhi"
        >
        </el-cascader>
      </el-form-item>

      <el-form-item label="生源地区" prop="work_shengyuan">
        <el-cascader
          size="large"
          :options="options"
          v-model="newUserInfo.work_shengyuan"
        >
        </el-cascader>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('newUserInfo')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('newUserInfo')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { uploadInfo } from "@/api/data";
import { regionData } from "element-china-area-data";
export default {
  data() {
    return {
      options1: [],
      options: regionData,
      newUserInfo: {
        work_username: "",
        work_xuehao: "",
        work_zhuanye: [],
        work_fangshi: "",
        work_xuezhi: "",
        work_xueli: "",
        work_shengyuan: [],
        work_kun_grade: "",
        work_jiuye: "",
        work_danwei: "",
        work_dizhi: [],
        work_sex: "",
        work_biye_time:""
      },
      rules: {
        work_biye_time:[{ required: true, message: "请选择毕业时间", trigger: "blur" }],
        work_sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        work_xuehao: [
          { required: true, message: "请输入学员的学号", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
        work_username: [{ message: "请输入毕业学员名字", trigger: "blur" }],
        work_shengyuan: [
          { required: true, message: "请选择生源地", trigger: "blur" },
        ],

        work_zhuanye: [
          { required: true, message: "请选择所属专业", trigger: "change" },
        ],
        work_fangshi: [
          { required: true, message: "请选择培养方式", trigger: "change" },
        ],
        work_xuezhi: [
          { required: true, message: "请选择学制", trigger: "change" },
        ],
        work_xueli: [
          { required: true, message: "请选择学历", trigger: "change" },
        ],
        work_kun_grade: [
          { required: true, message: "请选择困难级别", trigger: "change" },
        ],
        work_jiuye: [
          { required: true, message: "请选择就业情况", trigger: "change" },
        ],
      },
    };
  },
  async mounted() {
    if(this.$store.state.userdata.shengfen==0){
      this.newUserInfo.work_xuehao=this.$store.state.userdata.xuehao
    }
    // console.log(regionData)
    this.handleDizhi();
    this.options1 = JSON.parse(sessionStorage.getItem("structure"));
  },
  methods: {
    handleDizhi() {
      let arr = JSON.parse(JSON.stringify(this.options));
      for (let i = 0; i < arr.length; i++) {
        arr[i].value = arr[i].label;
        for (let j = 0; j < arr[i].children.length; j++) {
          arr[i].children[j].value = arr[i].children[j].label;
          if (!arr[i].children[j].children) {
            continue;
          }
          for (let a = 0; a < arr[i].children[j].children.length; a++) {
            arr[i].children[j].children[a].value =
              arr[i].children[j].children[a].label;
          }
        }
      }

      this.options = arr;
    },
    // 提交表单
    submitForm(formName) {
      // console.log(this.options)
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.newUserInfo.work_dizhi.length == 0) {
            this.newUserInfo.work_dizhi = ["升学"];
          }
       
          let data = await uploadInfo({...this.newUserInfo,shengfen:this.$store.state.userdata.shengfen});
          if (data.code == 200) {
            this.$refs["newUserInfo"].resetFields();
            return this.$message.success(data.msg);
          }
          this.$message.error(data.msg);
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.upload_cover {
  padding: 20px;
  max-width: calc(100vw - 200px);
  overflow: hidden;
}
.el-form {
  width: 100% !important;
}
.el-input {
  width: 220px;
}
.nav_title {
  height: 60px;
  line-height: 60px;
  color: #444;
  text-align: center;
  margin-bottom: 20px;
}
</style>